﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SiteFox.Business.Manager
{
    public static class Extension
    {
        public static void Create(Model.Extension _Extension, out string _Message)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            if (GetByExt(_Extension.Ext) == null)
            {
                _Entities.AddToExtension(_Extension);
                _Entities.SaveChanges();
                _Message = "New extension added.";
            }
            else 
            {
                _Message = "Extension already exist.";
            }
        }
        public static IList<Model.Extension> GetAll()
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.Extension select c;
            return _Query.ToList();
        }
        public static Model.Extension GetByExt(string _Ext)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.Extension where c.Ext == _Ext select c;
            return _Query.FirstOrDefault();
        }
        public static IList<Model.Extension> GetByCreateNew()
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.Extension where c.AllowCreate == true select c;
            return _Query.ToList();
        }
        public static void Edit(Model.Extension _Extension, out string _Message)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.Extension where c.ID == _Extension.ID select c;
            var __Extension = _Query.Single();
            __Extension.AllowCreate = _Extension.AllowCreate;
            __Extension.AllowDownload = _Extension.AllowDownload;
            __Extension.AllowUpload = _Extension.AllowUpload;
            __Extension.BigIconUrl = _Extension.BigIconUrl;
            __Extension.Ext = _Extension.Ext;
            __Extension.Name = _Extension.Name;
            __Extension.SmallIconUrl = _Extension.SmallIconUrl;
            __Extension.TemplateUrl = _Extension.TemplateUrl;
            __Extension.Hidden = _Extension.Hidden;
            _Entities.SaveChanges();
            _Message = "Extension saved.";
        }
        public static void Delete(int _ID, out string _Message)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.Extension where c.ID == _ID select c;
            var _Extension = _Query.First();
            _Entities.Extension.DeleteObject(_Extension);
            _Entities.SaveChanges();
            _Message = "Extension deleted.";
        }
    }
}
